package logic

import (
	"context"
	"fmt"
	"ware/models/mysql"

	"ware/warerpc/internal/svc"
	"ware/warerpc/warerpc"

	"github.com/zeromicro/go-zero/core/logx"
)

type RegisterLogic struct {
	ctx    context.Context
	svcCtx *svc.ServiceContext
	logx.Logger
}

func NewRegisterLogic(ctx context.Context, svcCtx *svc.ServiceContext) *RegisterLogic {
	return &RegisterLogic{
		ctx:    ctx,
		svcCtx: svcCtx,
		Logger: logx.WithContext(ctx),
	}
}

func (l *RegisterLogic) Register(in *warerpc.RegisterRequest) (*warerpc.RegisterResponse, error) {
	// todo: add your logic here and delete this line

	var user mysql.User

	mysql.DB.Debug().Where("mobile=?", in.Mobile).Find(&user)

	if user.ID == 0 {
		return nil, fmt.Errorf("手机号不存在,请前去注册")
	}

	m := mysql.User{

		Mobile:   in.Mobile,
		Password: in.Password,
	}
	mysql.DB.Debug().Create(&m)
	return &warerpc.RegisterResponse{
		UserId: int64(m.ID),
	}, nil
}
